﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.IO;

namespace DotNetImage.Imaging
{
    /// <summary>
    /// Represents file formats, supported by current library (depends on WPF codes and installed third-party codecs).
    /// </summary>
    public enum SupportedFileFormat
    {
        JPEG,
        BMP,
        PNG,
        TIFF,
        GIF,
        WMP
    }

    public static class SupportedFileFormatExtension
    {
        /// <summary>
        /// Returns proper encoder which is used for saving image to the disk or stream
        /// </summary>
        /// <param name="ff"></param>
        /// <returns></returns>
        public static BitmapEncoder GetEncoder(this SupportedFileFormat ff)
        {
            switch (ff)
            {
                case SupportedFileFormat.BMP:
                    return new BmpBitmapEncoder();
                case SupportedFileFormat.GIF:
                    return new GifBitmapEncoder();
                case SupportedFileFormat.JPEG:
                    return new JpegBitmapEncoder();
                case SupportedFileFormat.PNG:
                    return new PngBitmapEncoder();
                case SupportedFileFormat.TIFF:
                    return new TiffBitmapEncoder();
                case SupportedFileFormat.WMP:
                    return new WmpBitmapEncoder();
                default:
                    throw new NotSupportedException("Unsupported format " + Enum.GetName(typeof(SupportedFileFormat), ff));
            }
        }


        /// <summary>
        /// Returns supported format by given decoder (image reader).
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SupportedFileFormat GetDecoder(BitmapDecoder type)
        {
            if (type as JpegBitmapDecoder != null)
            {
                return SupportedFileFormat.JPEG;
            }
            if (type as PngBitmapDecoder != null)
            {
                return SupportedFileFormat.PNG;
            }
            if (type as BmpBitmapDecoder != null)
            {
                return SupportedFileFormat.BMP;
            }
            if (type as GifBitmapDecoder != null)
            {
                return SupportedFileFormat.GIF;
            }
            if (type as TiffBitmapDecoder != null)
            {
                return SupportedFileFormat.TIFF;
            }
            if (type as WmpBitmapDecoder != null)
            {
                return SupportedFileFormat.WMP;
            }
            throw new NotSupportedException("Unknown decoder " + type.GetType());
        }


        /// <summary>
        /// Returns supported file format by file extension
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static SupportedFileFormat fromFileExtension(string fileName)
        {
            string[] jpeg_extensions = { "JPEG", "JPG", "JPE", "JIF", "JFIF", "JFI" };
            string[] png_extensions = { "PNG" };
            string[] bmp_extensions = { "BMP", "DIB" };
            string[] tiff_extensions = { "TIF", "TIFF" };
            string[] wmp_extensions = { "WMP", "WDP" };
            string[] gif_extensions = { "GIF" };

            FileInfo fi = new FileInfo(fileName);
            string extension = fi.Extension;
            extension = string.IsNullOrWhiteSpace(extension) ? string.Empty : extension.Trim().TrimStart('.').ToUpperInvariant();
            if (jpeg_extensions.Contains(extension))
            {
                return SupportedFileFormat.JPEG;
            }
            if (png_extensions.Contains(extension))
            {
                return SupportedFileFormat.PNG;
            }
            if (bmp_extensions.Contains(extension))
            {
                return SupportedFileFormat.BMP;
            }
            if (tiff_extensions.Contains(extension))
            {
                return SupportedFileFormat.TIFF;
            }
            if (wmp_extensions.Contains(extension))
            {
                return SupportedFileFormat.WMP;
            }
            if (gif_extensions.Contains(extension))
            {
                return SupportedFileFormat.GIF;
            }

            throw new ArgumentException(fileName);
        }
    }
}
